/*
 */
package org.hook.showcase.persistence.mybatis;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.util.Assert;

/**
 * @author suwuqiang 2011-7-12
 */
public class MyBatisDemo01 {

	public static void main(String[] args) {
		try {
			String resource = "mybatis/configuration.xml";
			Reader reader = Resources.getResourceAsReader(resource);
			SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
			Assert.notNull(factory, "Build SqlSessionFactory fail.");
			Configuration configuration = factory.getConfiguration();
			Assert.notNull(configuration, "Configuration of SqlSessionFactory is Null.");
			Environment environment = configuration.getEnvironment();
			Assert.notNull(configuration, "Environment of Configuration is Null.");
			Assert.isTrue("development".equals(environment.getId()), "ID of Environment is Not Match.");
			SqlSession sqlSession = factory.openSession();
			Assert.notNull(sqlSession, "SqlSession of SqlSessionFactory is Null.");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
